var ctx=window.daijia.ctx;

$(document).ready(function(){
	
	$("#end").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});

	$('#companyIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    maxHeight:300,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择所属公司',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	$('#gradeIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    maxHeight:300,
	    buttonWidth: '100%',
	    nonSelectedText: '不限制客户等级',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	$('#orderFromSource').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择订单来源',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	$("input[name=freeType]").change(function(){
		$("#otherMoney_div").hide();
		if($(this).val()==2){
			$("#otherMoney_div").fadeIn(200);
			
		}
	});
	
	$("#submit_button").click(function(){
		if(main){
			if(null==$("#companyIds").val()){
				$.scojs_message("请选择所属公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#activityTarget").val()==""){
			$.scojs_message("请选择客户类型", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		/*if(null==$("#gradeIds").val()){
			$.scojs_message("请选择客户等级", $.scojs_message.TYPE_ERROR);
			return false;
		}*/
		
		if(null==$("#orderFromSource").val()){
			$.scojs_message("请选择订单来源", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#end").val()!=""){
			if($("#start").val()>$("#end").val()){
				 $.scojs_message("起始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
//		if($("input[name=sendType]:checked").length==0){
//			$.scojs_message("请选择发送方式", $.scojs_message.TYPE_ERROR);
//			return false;
//		}
		if(main){
			var companyNames=[];
			$("#companyIds option:selected").each(function(index,element){
				companyNames.push($(element).text());
			});
			$("#companyNames").val(companyNames.join(","));
		}
		
		var gradeNames=[];
		if($("#gradeIds option").size()==$("#gradeIds option:selected").size()){
			$("#gradeIds").val("");
			$("#gradeNames").val("");
			
		}else{
			$("#gradeIds option:selected").each(function(index,element){
				gradeNames.push($(element).text());
			});
			$("#gradeNames").val(gradeNames.join(","));
		}
		
		$("#submit_button").attr("disabled",true);
		$.post(ctx+'/activity/update',$("#submit_form").serialize(),function(data){
			if(data.success){
				$.scojs_message("修改成功", $.scojs_message.TYPE_OK);
				window.location=ctx+"/activity/list?event="+$("#activityEvent").val();
			}
			else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				$("#submit_button").attr("disabled",false);
			}
		});
		
	});

});